package common

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"reflect"
)

// Controller interface
type Controller interface {
	InitEngineV1(e *gin.Engine)
	InitRouterGroupV1(g *gin.RouterGroup)
	InitEngineV2(e *gin.Engine)
	InitRouterGroupV2(g *gin.RouterGroup)
}

var controllers map[string]Controller

func init() {
	controllers = make(map[string]Controller)
}

func getControllerName(c Controller) string {
	var ret string
	if c != nil {
		ret = reflect.TypeOf(c).String()
	}
	return ret
}

// PrintControllers print controllers
func PrintControllers() {
	for name := range controllers {
		fmt.Println(name, "=", controllers[name])
	}
}

// RegisterController register controller
func RegisterController(c Controller) {
	if c != nil {
		controllers[getControllerName(c)] = c
	}
}

// GetControllers get controllers
func GetControllers() map[string]Controller {
	return controllers
}
